package mxx.mr.job;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * @program:report-form-hadoop
 * @description:产品月度合格率统计表
 * @author:AlTuring
 * @Date:2019/11/13-14:34
 **/
public class QuailityMonthQualifiedJob {
    /**
     * 产品月度合格率统计表
     */
    public static class QuailityMonthQualifiedMapper extends Mapper<LongWritable, Text,Text,Text> {
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String[] datas = value.toString().split(",");
            //产品编码
            String productCode = datas[0];
            //工序SEQ
            String processSQE = datas[2];
            //检验时间
            String time = datas[5].substring(0,7);

            // id Hash key(工序id-检验时间)
            Text outKey = new Text(productCode +"_"+
                    processSQE + "_" +
                    time);

            context.write(outKey, value);
        }
    }
    /**
     * 产品月度合格率统计表
     */
    public static class QuailityMonthQualifiedReducer extends Reducer<Text, Text, Text, Text> {
        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            //产品编码
            String productCode = "";
            //产品名称
            String productName = "";
            //工序SEQ
            String processSQE = "";
            //工序编码
            String processCode = "";
            //工序名称
            String processName = "";
            //合格数量
            int qualifiedNum = 0;
            //不合格数量
            int unqualifiedNum = 0;
            //是否是成品检验，默认 不是成品工序
            String isFinishProduct = "0";
            //时间（2019-11）
            String time = "";
            for (Text value : values){
                String[] datas = value.toString().split(",");
                productCode = datas[0];
                productName = datas[1];
                processSQE = datas[2];
                processCode = datas[3];
                processName = datas[4];
                time = datas[5].substring(0,7);
                //判断该工序是否是成品检验
                if ("finished_test".equals(datas[6])){
                    isFinishProduct = "1";
                }
                //判断检验结果是否合格
                if ("y".equals(datas[7])){
                    qualifiedNum++;
                }else if ("n".equals(datas[7])){
                    unqualifiedNum++;
                }
            }
            //生成value
            Text outValue = new Text(productCode +","+
                    productName +","+
                    processSQE +","+
                    processCode +","+
                    processName +","+
                    qualifiedNum +","+
                    unqualifiedNum +","+
                    isFinishProduct +","+
                    time);
            // -- 使用联合key的hash散列作为key
            Text outKey = new Text(DigestUtils.shaHex(key.toString()));

            context.write(outKey,outValue);
        }
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration configuration = new Configuration();
        configuration.set("mapred.textoutputformat.separator", ",");

        // 如果输出目录已经存在，则先删除
        FileSystem fileSystem = FileSystem.get(configuration);

        // 输出文件
        Path outputPath = new Path(args[1]);
        if (fileSystem.exists(outputPath)) {
            fileSystem.delete(outputPath, true);
        }
        // 输入文件
        Path inputPath = new Path(args[0]);

        Job job = Job.getInstance(configuration);
        job.setJarByClass(QuailityMonthQualifiedJob.class);

        job.setMapperClass(QuailityMonthQualifiedMapper.class);
        job.setReducerClass(QuailityMonthQualifiedReducer.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        FileInputFormat.setInputPaths(job, inputPath);
        FileOutputFormat.setOutputPath(job,outputPath);

        job.waitForCompletion(true);

        System.out.println("---质量报表:月度合格率报表完后---");
    }
}
